package org.wmq.skyserver.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.wmq.skycommon.exception.DeleteNotAllowedException;
import org.wmq.skycommon.pojo.DTO.DishPageDTO;
import org.wmq.skycommon.pojo.DTO.DishDTO;
import org.wmq.skycommon.pojo.VO.DishVO;
import org.wmq.skycommon.pojo.entity.PageResult;
import org.wmq.skycommon.pojo.entity.Result;
import org.wmq.skyserver.service.DishService;

import java.io.IOException;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api("菜品接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        this.dishService.saveWithFlavor(dishDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result page(DishPageDTO dishPageDTO){
        PageResult result = this.dishService.pageQuery(dishPageDTO);
        return Result.success(result);
    }

    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Long> ids) throws DeleteNotAllowedException {
        this.dishService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result getById(@PathVariable Long id){
        DishVO dishVO = this.dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        this.dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(Long categoryId){
        return Result.success(this.dishService.list(categoryId));
    }
}
